KOI MicroPython API¶
KOI也支援使用MicroPython編程,可以實現純文字的編程。
基本應用¶
示範程式¶
#/bin/python
from koi import *
from time import sleep
x = 0
lcd.rotation(2) #改變屏幕方向
sleep(1)
lcd.rotation(0) #改變屏幕方向
sleep(1)
drawString(5,5,"hello world",500) #顯示字串
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue() == 1:
img.save("s1.jpg") #KOI拍照
if btnBValue() == 1:
loadImage("s1.jpg") #顯示相片
特徵分類器¶
示範程式¶
#模型訓練與保存
from koi import *
x=0
cla.reset()
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue():
cla.addImage('apple') #提取特徵添加標籤
if btnBValue():
cla.addImage('orange') #提取特徵添加標籤
if btnAValue() and btnBValue():
cla.save('fruit.json') #保存分類器
time.sleep(0.1)
#模型載入與運行
from koi import *
x=0
cla.reset() #初始化分類器
cla.load("fruit.json") #載入分類器
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue():
tag=cla.getImageTag() #執行特徵分類
if tag=='orange':
print('I like oranges.')
elif tag=='apple':
print('Apples are healthy.')
time.sleep(0.1)
人臉追蹤¶
示範程式¶
#/bin/python
from koi import *
x = 0
face_prop=[0,0]
yoloinit() #載入人臉模型
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
r = trackface() #人臉追蹤
if r:
is_face=1
drawString(5,5,r,500)
face_prop[0]=(r[0][2]+r[0][0])/2
face_prop[1]=(r[0][3]+r[0][1])/2
else:
is_face=0
while is_face:
print('X: '+str(face_prop[0]))
print('Y: '+str(face_prop[1]))
is_face=0
time.sleep(0.5)
幾何圖形識別¶
示範程式¶
from koi import *
x=0
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue() and btnBValue():
line_prop = findLines() #線條追蹤
print(line_prop[0])
time.sleep(0.1)
elif btnAValue():
circle_prop = findCircle(4000) #圓形追蹤
print(circle_prop[0])
time.sleep(0.1)
elif btnBValue():
rect_prop = findRects(4000) #矩形追蹤
print(rect_prop[0])
time.sleep(0.1)
顏色追蹤¶
示範程式¶
from koi import *
x=0
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue():
colorCalibrate() #顏色校正
time.sleep(0.1)
elif btnBValue():
blob_prop=findBlob() #追蹤色塊
print(blob_prop[0])
time.sleep(0.1)
elif btnAValue() and btnBValue():
line_prop=findLinearRegress() #追蹤巡線
print(line_prop[0])
time.sleep(0.1)
條碼識別¶
示範程式¶
from koi import *
x=0
lcd.rotation(2)
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue():
barcode=findBarCode() #Barcode識別
print(barcode[0][4])
time.sleep(0.1)
if btnBValue():
qrcode=findQRCode() #QR Code識別
print(qrcode[0][4])
time.sleep(0.1)
if btnAValue() and btnBValue():
april=findAprilTag() #AprilTag識別
print(april[0])
time.sleep(0.1)
語音辨識¶
參考程式¶
# 錄音與播放
from koi import *
x=0
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue():
speech.recordWav('hi.wav') #錄製wav音頻檔
time.sleep(0.1)
if btnBValue():
speech.playWav('hi.wav') #播放wav音頻檔
time.sleep(0.1)
#語音辨識
from koi import *
x=0
speech.noiseTap() #設立噪音基準
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue():
speech.addCommand("hello") #增加命令詞
time.sleep(0.1)
if btnBValue():
speech.addCommand("bye") #增加命令詞
time.sleep(0.1)
if btnAValue() and btnBValue():
print(speech.getCommand()) #運行語音辨識
time.sleep(0.1)
物聯網¶
參考程式¶
from koi import *
wifi.joinap(str("apname"),str("password")) #連接網絡
time.sleep(2)
print(wifi.ipaddr()) #獲取IP地址
time.sleep(2)
wifi.mqtthost("127.0.0.1") #連接MQTT伺服器
wifi.mqttsub("test01") #訂閱話題
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue():
wifi.mqttpub("test01","hello world") #發佈信息
if btnBValue():
msg=wifi.mqttread("test01") #讀取訊息
print("Message: "+msg[0])
print("Topic: "+msg[1])
人臉辨識¶
人臉參數¶
face['parameter']
獲取人臉辨識的參數。
parameter代表參數,可以獲得的參數有:
- face_token:人臉特徵碼
- location: 人臉的位置信息,包括座標和大小等
- gender:性別
- expression:表情
- angle:人臉傾斜角度
- mask:人臉是否有佩戴口罩
- age:年齡
參考程式¶
from koi import *
wifi.joinap(str("apname"),str("password"))
time.sleep(2)
while True:
img=sensor.snapshot() #屏幕刷新
lcd.display(img) #屏幕刷新
if btnAValue():
face=baiduFace(op=1)
time.sleep(5)
baiduFace(op=2, token=face['face_token'], group="group", name="name") #添加人臉到組別
if btnBValue():
face=baiduFace(op=1)
time.sleep(5)
result=baiduFace(op=3, token=face['face_token'], group="name") #在組別搜尋人臉
print("Name: "+faceResult['result']['user_list'][0]['user_id'])
print("Confidence: "+str(faceResult['result']['user_list'][0]['score']))